#include "PlaylistFunctionFactory.h"

#include <QAction>

#include "MediaEngine.h"
#include "PictureEngine.h"
#include "MediaListModel.h"
#include "MediaSourceFactory.h"
#include "AudioVideoAutomation.h"
#include "PictureAutomation.h"
#include "ApplicationIcon.h"

#include "MediaToolbar.h"

PlaylistFunctionFactory::PlaylistFunctionFactory(QObject *parent) :
   QObject(parent)
{
}

MediaListModel *PlaylistFunctionFactory::buildModel()
{
   MediaSourceFactory *mediaSourceFactory = new MediaSourceFactory();
   MediaListModel *model = new MediaListModel( mediaSourceFactory, this);

   return model;
}

IF_MediaEngineInterface *PlaylistFunctionFactory::buildAudioVideoEngine(/*MediaListModel *mediaModel*/)
{
   MediaEngine *engine = new MediaEngine( /*mediaModel,*/ this);
   return engine;
}

IF_PictureEngineInterface *PlaylistFunctionFactory::buildPictureEngine()
{
   PictureEngine *engine = new PictureEngine( this);
   return engine;
}

AudioVideoAutomation *PlaylistFunctionFactory::buildAutomation( MediaListModel *model,
                                                              IF_MediaEngineInterface *engine,
                                                              ActionListController *controller, Fader *fader,
                                                              ApplicationSettings & appSettings)
{
   AudioVideoAutomation *automation = new AudioVideoAutomation( engine, model, controller, fader,
                                                            appSettings, this);
   return automation;
}

PictureAutomation * PlaylistFunctionFactory::buildPictureAutomation( MediaListModel *model,
                                                                     IF_PictureEngineInterface *engine,
                                                                     ActionListController *controller)
{
   PictureAutomation * automation = new PictureAutomation( engine, model, controller, this);
   return automation;
}

QList<QAction *> PlaylistFunctionFactory::buildActionList(IF_MediaEngineInterface *engine,
                                                          AudioVideoAutomation *automation)
{
   QList<QAction *> actionList;

   QAction *playAction = new QAction( QIcon(IconPath("track_play.png")), tr("play media"), this);
   playAction->setShortcut( Qt::SHIFT | Qt::Key_P);
   playAction->setEnabled( false);
   connect( playAction, SIGNAL(triggered()), engine, SLOT(play()) );

   QAction *pauseAction = new QAction( QIcon(IconPath("track_pause.png")), tr("pause media"), this);
   playAction->setEnabled( false);
   pauseAction->setShortcut( Qt::SHIFT | Qt::Key_Space);
   connect( pauseAction, SIGNAL(triggered()), engine, SLOT(pause()) );

   QAction *togglePlayAction = new QAction( tr("play/pause media"), this);
   togglePlayAction->setShortcut( Qt::Key_Space);
   connect( togglePlayAction, SIGNAL(triggered()), engine, SLOT(togglePlayPause()) );

   MediaToolbar *mediaTools = new MediaToolbar( playAction, pauseAction, this);
   connect( engine, SIGNAL(stateChanged(AudioObject::State)), mediaTools,
            SLOT(onMediaStateChanged(AudioObject::State)) );

   QAction *muteAction = new QAction( tr("toggle Mute"), this);
   muteAction->setShortcut( Qt::Key_M);
   muteAction->setCheckable( true);
   muteAction->setChecked( false);
   connect( muteAction, SIGNAL(triggered(bool)), engine, SLOT(setMuted(bool)) );
   connect( engine, SIGNAL(muteStatus(bool)), muteAction, SLOT(setChecked(bool)) );

   QAction *rewindAction = new QAction( tr("rewind"), this);
   rewindAction->setShortcut( Qt::Key_Home);
   connect( rewindAction, SIGNAL(triggered()), engine, SLOT(rewind()) );

   QAction *toggleFadeAction = new QAction( QIcon(IconPath("fade_out.png")),
                                            tr("run/stop fading"), this);
   toggleFadeAction->setShortcut( Qt::Key_F);
   connect( toggleFadeAction, SIGNAL(triggered()), automation, SLOT(toggleFader()) );

   QAction *defaultVolumeAction = new QAction( QIcon(IconPath("def_vol.png")),
                                               tr("apply default volume"), this);
   defaultVolumeAction->setShortcut( Qt::Key_V);
   connect( defaultVolumeAction, SIGNAL(triggered()), automation, SLOT(applyDefaultVolume()) );

   actionList << togglePlayAction << playAction << pauseAction << muteAction
              << rewindAction << toggleFadeAction << defaultVolumeAction;

   return actionList;
}
